package ezonsport.android.ui.stepSystem.dataHistory;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import ezonsport.android.core.stepSystem.StepData;
import ezonsport.android.core.systemManage.User;
import ezonsport.android.data.DataHelper;
import ezonsport.android.service.SportDataService;
import ezonsport.android.service.SystemManageService;

/**
 * 历史数据控制
 * @author 桑成龙
 *
 */
public class DataHistoryController {

	private DataHistoryViewModel dataHistoryModel;
	private SystemManageService systemManageService;
	private SportDataService sportDataService;
	/**
	 * 构造函数
	 * @param dataHistoryModel
	 */
	public DataHistoryController(DataHelper dataHelper) {
		this.dataHistoryModel = new DataHistoryViewModel();
		sportDataService = new SportDataService(dataHelper);
		systemManageService = new SystemManageService(dataHelper);

	}
	
	/**
	 * 获取历史记录
	 * @param fromDate：历史开始时间
	 * @param toDate：历史结束时间
	 * @return DataHistoryViewModel
	 */
	public DataHistoryViewModel getDataHistory(Date fromDate,Date toDate){
		List<StepData> stepDatas=sportDataService.getStepData(fromDate, toDate);
		Collections.reverse(stepDatas);
		this.dataHistoryModel.setHistoryData(stepDatas);
		return this.dataHistoryModel;
	}
	
	/**
	 * 获取历史记录
	 * @param fromDate：开始日期
	 * @param toDate：结束日期
	 * @param userName：要获取的用户名
	 * @return DataHistoryViewModel
	 */
	public DataHistoryViewModel getDataHistory(Date fromDate,Date toDate,String userName){
//		已知体重、距离 
//		跑步热量（kcal）＝体重（kg）×距离（公里）×1.036 
		User user = systemManageService.getUserInfo(userName);
		List<StepData> stepDatas=sportDataService.getStepData(fromDate, toDate);
		Collections.reverse(stepDatas);
		for(int i=0;i<stepDatas.size();i++){
			double dis = stepDatas.get(i).getDistance()/1000.0;
			int cal =(int)( user.getWeight() * dis * 1.036);
			stepDatas.get(i).setCalories(cal);
		}
		this.dataHistoryModel.setHistoryData(stepDatas);
		return this.dataHistoryModel;
	}
	
	
	
}
